package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.MobilePaymentRecord;
import com.kichina.parking.service.logic.service.MobilePaymentRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 移动支付记录
 */
@Component
@Slf4j
public class Parking12MobileChargeUpstreamHandler {

    private final MobilePaymentRecordService mobilePaymentRecordService;

    public Parking12MobileChargeUpstreamHandler(MobilePaymentRecordService mobilePaymentRecordService) {
        this.mobilePaymentRecordService = mobilePaymentRecordService;
    }

    /**
     * @param jsonObject 数据实体
     * @author Surging
     * @Description 移动支付记录上传信息到服务端；
     * 上传方式：立即上传。每次上次 只能100 条逐条上传。
     * @Return void
     * @date 20:53 2022-09-15
     **/
    public void recordParkingMobileChargeUpstream(JSONObject jsonObject) throws JsonParseException {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        MobilePaymentRecord mobilePaymentRecord;
        try {
            mobilePaymentRecord = JsonToEntityUtil.jsonToEntity(jsonObject, MobilePaymentRecord.class);
            if (Objects.nonNull(jsonObject.getString("in_time"))){
                if(String.valueOf(jsonObject.getString("in_time")).length()<15) {
                    //字符串格式两种 2022-10-15; 2022-09-16 15:54:00;
                    mobilePaymentRecord.setInTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("in_time"))));
                }else {
                    mobilePaymentRecord.setInTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("in_time"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("begin_time"))) {
                if(String.valueOf(jsonObject.getString("begin_time")).length()<15) {
                    mobilePaymentRecord.setBeginTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("begin_time"))));
                }else {
                    mobilePaymentRecord.setBeginTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("begin_time"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("end_time"))) {
                if(String.valueOf(jsonObject.getString("end_time")).length()<15) {
                    mobilePaymentRecord.setEndTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("end_time"))));
                }else {
                    mobilePaymentRecord.setEndTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("end_time"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("pay_time"))) {
                if(String.valueOf(jsonObject.getString("pay_time")).length()<15) {
                    mobilePaymentRecord.setPayTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("pay_time"))));
                }else {
                    mobilePaymentRecord.setPayTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("pay_time"))));
                }
            }  if (Objects.nonNull(jsonObject.getString("save_time"))) {
                if(String.valueOf(jsonObject.getString("save_time")).length()<15) {
                    mobilePaymentRecord.setSaveTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("save_time"))));
                }else {
                    mobilePaymentRecord.setSaveTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("save_time"))));
                }
            }
            LambdaQueryWrapper<MobilePaymentRecord> eq = new QueryWrapper<MobilePaymentRecord>().lambda()
                    .eq(MobilePaymentRecord::getParkid, mobilePaymentRecord.getParkid())
                    .eq(MobilePaymentRecord::getCarNumber, mobilePaymentRecord.getCarNumber())
                    .eq(MobilePaymentRecord::getPayTime, mobilePaymentRecord.getPayTime())
                    .eq(MobilePaymentRecord::getMobilechargeid, mobilePaymentRecord.getMobilechargeid());
            List<MobilePaymentRecord> blacklistInfoRecords = mobilePaymentRecordService.list(eq);
            if (CollectionUtils.isNotEmpty(blacklistInfoRecords)){
                //更新
                mobilePaymentRecord.setUpdateTime(new Date());
                mobilePaymentRecordService.update(mobilePaymentRecord, eq);
            }else {
                //添加
                mobilePaymentRecord.setMobilePaymentRecordId(UUIDUtil.getUUID());
                mobilePaymentRecord.setUpdateTime(new Date());
                //原实体有createTime字段，serviceCreateTime作区分
                mobilePaymentRecord.setCreateTime(new Date());
                mobilePaymentRecordService.save(mobilePaymentRecord);
            }
        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
